package com.oda.camera.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.oda.resultats.util.Resultat;

public class SaveResultat {
	
	private static final String  FILE="colzaAllResult.xml";
	
	/**
	 * Tags for xml
	 */
	private static final String TAG_APPS="<oda>";
	private static final String TAG_END_APPS="</oda>";
	
	private static final String TAG_RES="<resultat>";
	private static final String TAG_END_RES="</resultat>";
	
	private static final String TAG_DATE="<date>";
	private static final String TAG_END_DATE="</date>";
	
	private static final String TAG_BIOMASSE="<biomasse>";
	private static final String TAG_END_BIOMASSE="</biomasse>";
	
	private static final String TAG_LAI="<lai>";
	private static final String TAG_END_LAI="</lai>";
	
	private static final String TAG_ER="<errorRate>";
	private static final String TAG_END_ER="</errorRate>";
	
	private static final String TAG_IDRES="<idResultat>";
	private static final String TAG_END_IDRES="</idResultat>";
	
	private static final String TAG_IDUSER="<idUser>";
	private static final String TAG_END_IDUSER="</idUser>";
	
	private static final String TAG_EXP="<expAzote>";
	private static final String TAG_END_EXP="</expAzote>";
	
	private static final String DIR_IMAGE="ColzaAllResult";
	
	/**
	 * the buffer
	 */
	private StringBuffer xml;
	/**
	 * Class for Album
	 */
	private AlbumStorageDirFactory mAlbumStorageDirFactory = null;
	
	/**
	 * 
	 * @param path create the xml to save path
	 */
	public SaveResultat(List<Resultat> res) {
		super();
		
        
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
			mAlbumStorageDirFactory = new FroyoAlbumDirFactory();
		} else {
			mAlbumStorageDirFactory = new BaseAlbumDirFactory();
		}
		
		xml=new StringBuffer();
		xml.append("<?xml version=\""+1.0+"\" encoding=\"UTF-"+8+"\"?>\n"
				   +TAG_APPS+"\n");
		for(Resultat s:res){
			xml.append(TAG_RES+"\n");
				xml.append(TAG_DATE+s.getDate()+TAG_END_DATE+"\n");	
				xml.append(TAG_BIOMASSE+s.getBiomasse()+TAG_END_BIOMASSE+"\n");	
				xml.append(TAG_ER+s.getErrorRate()+TAG_END_ER+"\n");
				xml.append(TAG_EXP+s.getExpAzote()+TAG_END_EXP+"\n");
				xml.append(TAG_IDRES+s.getIdResultat()+TAG_END_IDRES+"\n");
				xml.append(TAG_IDUSER+s.getIdUser()+TAG_END_IDUSER+"\n");				
				xml.append(TAG_LAI+s.getLai()+TAG_END_LAI+"\n");				
			xml.append(TAG_END_RES+"\n");

		}
		xml.append(TAG_END_APPS);
		
		try {			
			creatFile(FILE, xml.toString());
			
		} catch (IOException e) {
			Log.e("ERROR", "error to create file");
		}
		
	}
	
	public void creatFile(String nomfichier, String data) throws IOException{
		Log.d("FILE",getAlbumDir().toString() );
		File file = new File(getAlbumDir().toString()+"/"+nomfichier);
		BufferedWriter output = new BufferedWriter(new FileWriter(file));
		output.write(data);
		output.close();
		System.out.println("file save to: "+file.getAbsolutePath());
	}
	

	private File getAlbumDir() {
		File storageDir = null;

		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {

			storageDir = mAlbumStorageDirFactory
					.getAlbumStorageDir(DIR_IMAGE);

			if (storageDir != null) {
				if (!storageDir.mkdirs()) {
					if (!storageDir.exists()) {
						Log.d("CameraSample", "failed to create directory");
						return null;
					}
				}
			}

		} else {
			Log.v("",
					"External storage is not mounted READ/WRITE.");
		}

		return storageDir;
	}

}
